/**
 * @param {org.acme.mynetwork.Trade} trade - the trade to be processed
 * @transaction
 */
function tradeCommodity(trade) {
    trade.commodity.owner = trade.newOwner;
    return getAssetRegistry('org.acme.mynetwork.Commodity')
        .then(function (assetRegistry) {
            return assetRegistry.update(trade.commodity);
        });
}

/**
 * @param {org.acme.mynetwork.Persion} persion - the persion to be processed
 * @transaction
 */
function savePersion(persion) {

    let businessNetworkConnection = /* TODO: get a business network connection */
    businessNetworkConnection.getParticipantRegistry('org.acme.mynetwork')
        .then((participantRegistry) => {
            let factory = businessNetworkConnection.getFactory();
            let participant = factory.newResource('org.acme.mynetwork', 'Trader', persion.id);
            participant.firstName = persion.firstName;
            participant.address = persion.address;
            return participantRegistry.add(participant);
        })
        .catch((error) => {
            throw new Error("保存失败");
        });

}

/**
 * @param {org.acme.mynetwork.Auth} auth - the auth to be processed
 * @transaction
 */
function authPersion(auth) {
    let businessNetworkConnection = /* TODO: get a business network connection */
    businessNetworkConnection.issueIdentity('org.acme.mynetwork.Trader#'+auth.id, auth.firstName)
        .then((result) => {
            console.log(`userID = ${result.userID}`);
            console.log(`userSecret = ${result.userSecret}`);
        });
    }

/**
 * @param {org.acme.mynetwork.Remove} remove - the remove to be processed
 * @transaction
 */
function removePersion(remove) {
    let businessNetworkConnection = /* TODO: get a business network connection */
    businessNetworkConnection.revokeIdentity(remove.firstName)
        .then(() => {
            // Identity is no longer valid.
            throw new Error("失败");
        });
    }